package com.laikan.reader.core.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanBaseAdapter;
import com.laikan.reader.core.bean.CommentBean;
import com.laikan.reader.core.utils.PublicUtil;

import java.util.ArrayList;

import static com.laikan.reader.R.id.tv_reply;

@SuppressWarnings ("rawtypes")
public class BookDesCommentAdapter extends LaikanBaseAdapter<CommentBean> {

    ViewHolder holder = null;
    private ItemClickListener itemClickListener;
    boolean support;

    public BookDesCommentAdapter(Context context, ArrayList<CommentBean> datas) {
        super(context, datas);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.bookdes_comment_item, null);
            holder.view_divider = convertView.findViewById(R.id.view_divider);
            holder.head_reviewer_img = (SimpleDraweeView)convertView.findViewById(R.id.head_reviewer_img);
            holder.head_comment_content = (TextView)convertView.findViewById(R.id.head_comment_content);
            holder.comment_title = (TextView)convertView.findViewById(R.id.comment_title);
            holder.head_comment_time = (TextView)convertView.findViewById(R.id.head_comment_time);
            holder.comm_username = (TextView)convertView.findViewById(R.id.comm_username);
            holder.iv_click_to_support = (ImageView)convertView.findViewById(R.id.iv_clicktosupport);
            holder.iv_reply = (ImageView)convertView.findViewById(R.id.iv_reply);
            holder.tv_click_to_support = (TextView)convertView.findViewById(R.id.tv_clicktosupport);
            holder.tv_reply = (TextView)convertView.findViewById(tv_reply);
            holder.lr_item_content = (RelativeLayout)convertView.findViewById(R.id.lr_item_content);
            holder.rl_clicktosupport = (RelativeLayout)convertView.findViewById(R.id.rl_clicktosupport);
            holder.rl_clickreply = (RelativeLayout)convertView.findViewById(R.id.rl_clickreply);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }

        if (datas.get(position).isFavor()) {
            holder.iv_click_to_support.setImageDrawable(new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.iv_click_to_support), Color.parseColor("#66ccdd"))));
        }else {
            holder.iv_click_to_support.setImageDrawable(new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.iv_click_to_support), Color.parseColor("#999999"))));
        }
        holder.iv_reply.setImageDrawable(new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.iv_reply), Color.parseColor("#999999"))));

        if(!TextUtils.isEmpty(datas.get(position).getReplyCount()) && datas.get(position).getReplyCount().length() > 2){
            holder.tv_reply.setText(R.string.tv_comment_more);
        }else {
            holder.tv_reply.setText(datas.get(position).getReplyCount());
        }

        if(!TextUtils.isEmpty(datas.get(position).getLikeCount()) && datas.get(position).getLikeCount().length() > 2){
            holder.tv_click_to_support.setText(R.string.tv_comment_more);
        }else {
            holder.tv_click_to_support.setText(datas.get(position).getLikeCount());
        }
        holder.rl_clicktosupport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                itemClickListener.onClickToSupport(view, position);
                ImageView clickToSupport = (ImageView)((RelativeLayout)view).findViewById(R.id.iv_clicktosupport);
            }
        });

        holder.lr_item_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onReplyClick(view, position);
            }
        });
                holder.rl_clickreply.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        itemClickListener.onReplyClick(view, position);
                    }
                });
        if (datas.size() - 1 == position) {
            holder.view_divider.setVisibility(View.GONE);
        }else {
            holder.view_divider.setVisibility(View.VISIBLE);
        }
//        holder.head_comment_time.setText(PublicUtil.getDateToString(Long.parseLong(datas.get(position).getCreateTime())));
        holder.head_comment_time.setText(datas.get(position).getReviewShowTime());
        holder.head_reviewer_img.setImageURI(Uri.parse(datas.get(position).getUserIcon()));

        holder.head_comment_content.setText(datas.get(position).getContent().replace(" ","").replace(" ","").replace("　","").replace("　",""));
        if(TextUtils.isEmpty(datas.get(position).getName())){
            holder.comment_title.setVisibility(View.GONE);
        }else {
            holder.comment_title.setVisibility(View.VISIBLE);
            holder.comment_title.setText(datas.get(position).getName());
        }
        holder.comm_username.setText(datas.get(position).getUserName());
        return convertView;
    }

    private class ViewHolder {

        private SimpleDraweeView head_reviewer_img;
        private TextView comment_title, head_comment_time, head_comment_content, comm_username, tv_click_to_support, tv_reply;
        private ImageView iv_click_to_support, iv_reply;
        private View view_divider;
        private RelativeLayout lr_item_content, rl_clicktosupport, rl_clickreply;
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.itemClickListener = listener;
    }

    public interface ItemClickListener {

        void onItemClick(View view, int position);

        void onClickToSupport(View view, int position);

        void onReplyClick(View view, int position);
    }
}
